package com.zsjp.alc.restresult;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zsjp.alc.exception.ZsjpException;
import com.zsjp.alc.init.ZsjpRunner;

@ControllerAdvice
public class ZsjpExceptionResult {
    //日志打印类
    //private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionCatch.class);
 
    //捕获 CustomException异常
    @ExceptionHandler(ZsjpException.class)
    @ResponseBody   //响应json数据
    public ZsjpResponseResult sendResponse(ZsjpException e){
    	System.out.println("进入张师驾培异常处理返回类");
    	
        //获取错误错误码，根据错误码得到详细的错误信息
        String errorCode = e.getErrorCode();
        
        //创建响应类,也就是最终传给页面的数据   
        Object data = null;
        if(ZsjpRunner.ERROR_MAP.containsKey(errorCode)){
        	data = ZsjpRunner.ERROR_MAP.get(errorCode);
        }else{
        	// 统一返回-500未知错误
        	data = ZsjpRunner.ERROR_MAP.get("-500");
        }
        
        //构造返回数据
        ZsjpResponseResult response =  new ZsjpResponseResult(errorCode, data);
        return response;
    }
 
}
